package com.moyu.myadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moyu.myadmin.model.dto.SysPermissionDTO;
import com.moyu.myadmin.model.vo.SysPermissionVO;
import com.moyu.myadmin.model.vo.SysRolePermissionVO;
import com.moyu.myadmin.service.SysPermissionService;
import com.moyu.myadmin.utils.QueryData;
import com.moyu.myadmin.utils.ResultData;
import com.moyu.myadmin.utils.ReturnCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;

/**
 * 角色权限
 *
 * @email jiaomingyu5778@gmail.com
 * @date 2022-02-11 10:33:30
 */
@Validated
@Log4j2
@Api(tags = {"角色权限接口"})
@RestController
@RequestMapping("/sysPermission/")
public class SysPermissionController {

    private final SysPermissionService service;

    @Autowired
    public SysPermissionController(SysPermissionService service) {
        this.service = service;
    }

    @ApiOperation(value = "分页查询角色权限信息")
    @PostMapping("queryListPage")
    public ResultData<Page<SysPermissionVO>> queryList(@RequestBody QueryData<SysPermissionDTO> queryData) {
        log.info("角色权限查询，查询信息：{}" , queryData);
        Page<SysPermissionVO> page = service.queryListPage(queryData);
        return ResultData.success(page);
    }

    @ApiOperation(value = "保存角色权限信息")
    @PostMapping("save")
    public ResultData<String> save(@ApiParam(value = "角色权限信息") @Valid @RequestBody SysPermissionDTO dto) {
        log.info("角色权限保存，保存信息：{}" , dto);
        boolean update = service.save(dto);
        return update ? ResultData.success(ReturnCode.RC200.getMessage()) : ResultData.error(ReturnCode.RC999.getMessage());
    }

    @ApiOperation(value = "批量保存角色权限信息")
    @PostMapping("saves")
    public ResultData<String> saves(@Valid @NotNull @Size(min = 1, message = "主键长度最小为1") @RequestBody List<SysPermissionDTO> list) {
        log.info("批量角色权限保存，保存信息：{}" , list);
        boolean update = service.saveOrUpdateBatch(list);
        return update ? ResultData.success(ReturnCode.RC200.getMessage()) : ResultData.error(ReturnCode.RC999.getMessage());
    }

    @ApiOperation(value = "根据ID，批量删除角色权限信息")
    @PostMapping("/deletes")
    public ResultData<String> deletes(@Valid @NotNull @Size(min = 1, message = "主键长度最小为1") @RequestBody List<String> permissionIds) {
        boolean remove = service.removeBatchByIds(permissionIds);
        return remove ? ResultData.success(ReturnCode.RC200.getMessage()) : ResultData.error(ReturnCode.RC999.getMessage());
    }

    @ApiOperation(value = "根据ID，删除角色权限信息")
    @PostMapping("/delete")
    public ResultData<String> delete(@Valid @NotEmpty(message = "ID不能为空") @RequestBody String permissionId) {
        boolean remove = service.removeById(permissionId);
        return remove ? ResultData.success(ReturnCode.RC200.getMessage()) : ResultData.error(ReturnCode.RC999.getMessage());
    }

    @ApiOperation(value = "根据角色ID，删除角色权限信息")
    @PostMapping("/removeByRoleId")
    public ResultData<String> removeByRoleId(@Valid @NotEmpty(message = "ID不能为空") @RequestBody String roleId) {
        boolean remove = service.removeByRoleId(roleId);
        return remove ? ResultData.success(ReturnCode.RC200.getMessage()) : ResultData.error(ReturnCode.RC999.getMessage());
    }

    @ApiOperation(value = "根据角色ID查询授权信息")
    @GetMapping("/queryPermissionByRoleId")
    public ResultData<SysRolePermissionVO> queryPermissionByRoleId(@Valid @NotEmpty(message = "ID不能为空") String roleId) {
        SysRolePermissionVO vo = service.queryPermissionByRoleId(roleId);
        return ResultData.success(vo);
    }


}
